package bud.mobile.controller;

import javax.inject.Inject;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import bud.mobile.service.IMobileMemberService;
import bud.mobile.service.IMobileMemberService.MemberResult;
import bud.vo.MemberDTO;

@Controller
public class MainController {
	
	@Inject
	private IMobileMemberService service;
	public IMobileMemberService getService() {
		return service;
	}
	public void setService(IMobileMemberService service) {
		this.service = service;
	}

	@RequestMapping("login")
	public String login(){
		return "login";
	}
	
	@RequestMapping("logout")
	public String logout(HttpServletRequest request){
		HttpSession session = request.getSession();
		session.invalidate();
		return "login";
	}
	
	@RequestMapping("join")
	public String join(){
		return "join";
	}
	
	@RequestMapping("insertMember")
	public String insertMember(@ModelAttribute("member") MemberDTO member, Model model){
		
		MemberResult result = service.insertMember(member);
		String viewName = "";
		
		if(result == MemberResult.OK){
			model.addAttribute("msg","정상적으로 가입되었습니다!");
			viewName = "login";
		} else if (result == MemberResult.EmailDuplicated) {
			model.addAttribute("msg","이미 등록된 이메일입니다");
			viewName = "join";
		} else {
			model.addAttribute("msg","가입실패");
			viewName = "join";
		}
		
		return viewName;
	}
	
	@RequestMapping("loginChk")
	public String loginChk(@ModelAttribute("member") MemberDTO member, String check, 
			HttpServletRequest request, HttpServletResponse response, Model model){
		
		String viewName = null;

		if(StringUtils.isNotBlank(member.getMem_email()) && StringUtils.isNotBlank(member.getMem_pass())){
			MemberDTO tempMember = service.getMemberByEmail(member.getMem_email());
			if (tempMember==null){
				model.addAttribute("msg", "존재하지 않는 이메일입니다");
				viewName = "login";
				return viewName;
			}
			
			if(member.getMem_pass().equals(tempMember.getMem_pass())){
				Cookie idCookie = new Cookie("saveEmail", member.getMem_email());
				int expiry = 0;
				if(StringUtils.isNotBlank(check)){
					expiry = 60*60*24*7;
				}
				idCookie.setMaxAge(expiry);
				idCookie.setPath("/");
				response.addCookie(idCookie);
				
				// 로그인 상태를 유지하기 위해 session scope에 로그인 아이디를 유지해줌.
				HttpSession session = request.getSession();		// session 내장객체를 가져온다.
				session.setAttribute("memberSession", tempMember);
//				session.setAttribute("shopList", service.getShopList());
				viewName = "redirect:/scan/scanMap.mo";
			} else {
				model.addAttribute("msg", "패스워드가 일치하지 않습니다");
				model.addAttribute("member", member);
				viewName = "login";
			}
		} else {
			model.addAttribute("msg", "아이디나 패스워드가 누락되었습니다");
			model.addAttribute("member", member);
			viewName = "login";
		}
		return viewName;
	}
	
}
